package com.haotunet.app.goldtreasure.activity;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.VaultDetailAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.VaultRecordModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class MyVaultDetailAct extends BaseActivity {
	private TitleView mTitleView;
	private LinearLayout noRecordLayout;
	private PullableListView mLv;
	private PullToRefreshLayout mfreshLayout;
	private int pageNum = 20;
	private final int GET_VAULTRECORD_INFO_SUCCESS = 2;
	private final int LOADMORE_VAULTRECORD_INFO_SUCCESS = 3;
	private ActionUtil actionUtil;
	private VaultDetailAdapter vaultDetailAdapter;
	private Handler handle = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GET_VAULTRECORD_INFO_SUCCESS:
				VaultRecordModel model = (VaultRecordModel) msg.obj;
				if (model.getTransactionsDetails().size() == 0) {
					noRecordLayout.setVisibility(View.VISIBLE);
					return;
				}
				vaultDetailAdapter = new VaultDetailAdapter(MyVaultDetailAct.this, model.getTransactionsDetails());
				vaultDetailAdapter.setDatas(model.getTransactionsDetails());
				mLv.setAdapter(vaultDetailAdapter);
				break;
			case LOADMORE_VAULTRECORD_INFO_SUCCESS:
				VaultRecordModel moreModel = (VaultRecordModel) msg.obj;
				if (moreModel.getTransactionsDetails().size() == 0) {
					UIHelper.showToast(MyVaultDetailAct.this, getResources().getString(R.string.last_page_hint));
				}
				vaultDetailAdapter.addDatas(moreModel.getTransactionsDetails());
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buy_gold_detail);
		initView();
		getVaultRecordInfo("0", "");
	}

	private void RefreshData(final int pagenum) {
		mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				getVaultRecordInfo(pagenum + "", "refresh");
				pageNum = 20;
			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
				getVaultRecordInfo(pageNum + "", "loadmore");
			}
		});
	}

	private void getVaultRecordInfo(String pagenum, final String action) {
		actionUtil = new ActionUtil(MyVaultDetailAct.this);
		actionUtil.getVaultRecordInfo(pagenum);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (isFinishing()){
					return;
				}
				VaultRecordModel vaultModel = (VaultRecordModel) model;
				if (vaultModel == null) {
					UIHelper.showToast(MyVaultDetailAct.this, getString(R.string.rank_fail));
					return;
				}
				if (vaultModel.getErrorMsg() != null) {
					if (vaultModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(MyVaultDetailAct.this, getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(MyVaultDetailAct.this, vaultModel.getErrorMsg());
					}

				} else {
					if (action.equals("refresh") || action.equals("loadmore")) {
						mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					Message msg = new Message();
					if (action.equals("loadmore")) {
						msg.what = LOADMORE_VAULTRECORD_INFO_SUCCESS;
						pageNum = pageNum + 20;
					} else {
						msg.what = GET_VAULTRECORD_INFO_SUCCESS;
					}
					msg.obj = vaultModel;
					handle.sendMessage(msg);
				}
			}
		});
	}

	private void initView() {
		mfreshLayout = (PullToRefreshLayout) findViewById(R.id.id_swipe_ly);
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText(getString(R.string.my_gold_detail));

		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		noRecordLayout = (LinearLayout) findViewById(R.id.no_record_layout);
		mLv = (PullableListView) findViewById(R.id.lv_buy_gold_record);
		RefreshData(0);
	}
}
